﻿
@{
    ViewBag.Title = "GetAllProjectHistory";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>GetAllProjectHistory</h2>



<div id="ProductHistoryRequestGrid"></div>


<script type="text/javascript">
    var IsAssEmpChecked = false, assignedEmployeeIdVar = null;

    var editedUID = "";
  //  function showProjects(companyId, e) {
        var gridInEditMode = false;
        
        $('#ProductHistoryRequestGrid').show();

        $('#ProductHistoryRequestGrid').empty();
        var dataSource2 = new kendo.data.DataSource({
            transport: {
                read: {
                    type: "get",
                    url: "../ProductRequestGrid/GetAllProjects",
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    cache: false

                }
            },
            
            schema: {
                model: {
                    id: "id",
                    fields: {
                        id: {
                            editable: false, nullable: true,
                            type: "int"
                        },

                        contracts:
                            {
                                type: "int"
                            },

                        task:
                            {
                                type: "int"
                            },

                      
                        prName: {
                            type: "string",
                            validation: { required: true }
                        },
                        product: {
                            type: "string",
                            validation: { required: true }

                        },
                        CommEmployee:
                            {
                                type: "string",

                            },
                        name: {
                            type: "string",
                            validation: { required: true }
                        },
                        statusName: {
                            type: "string",
                            validation: { required: true }

                        },
                        paidCompany:
                            {
                                type: "string",

                            },

                        daysToFinish: {
                            type: "int"

                        },
                        depositAmount: {
                            editable: true,
                            type: "int"
                        },

                        estimatedCost: {
                            type: "int",

                        },
                        actualCost: {
                            type: "int",

                        },
                        paidInFull:
                        {
                            type: "boolean",
                            defaultValue: false,
                            filterable: {
                                messages: {
                                    isTrue: "Active", // sets the text for "isTrue" radio button
                                    isFalse: "Inactive",
                                }
                            }
                        },
                        paidByName:
                            {
                                type: "string"
                            },
                        taskScheduleComplete:
                      {
                          type: "boolean",
                          defaultValue: false,
                          filterable: {
                              messages: {
                                  isTrue: "Active", // sets the text for "isTrue" radio button
                                  isFalse: "Inactive",
                              }
                          }
                      },
                        contracts:
                        {
                            editable: false,
                            nullable: true
                        },
                        description: {

                            type: "string",
                            //validation: { required: true }
                        },

                        contractSentDate: {
                            type: "date",
                            nullable: true,
                            value: "",
                            //defaultOption: null
                        },
                        contractSignedDate: {
                            type: "date",

                            nullable: true,
                            value: "",
                            //defaultOption: null
                        },

                        departmentId:
                        {
                            type: "int"
                        },

                        depositReceivedDate:
                            {
                                type: "date",

                                nullable: true,
                                value: "",
                                defaultOption: null
                            },
                        startDate: {
                            type: "date",

                            nullable: true,
                            value: "",
                            defaultOption: null
                        },
                        expectedDeliveryDate:
                            {
                                type: "date",

                                nullable: true,
                                value: "",
                                defaultOption: null
                            },
                        deliveryDate: {
                            type: "date",

                            nullable: true,
                            value: "",
                            defaultOption: null
                        },
                        note: {
                            type: "string"

                        },

                        goLiveDate: {
                            type: "date",

                            nullable: true,
                            value: "",
                            defaultOption: null
                        },
                        projectManagerName:
                            {
                                type: "string",
                                //validation: { required: true }

                            },
                        leadDeveloperName: {
                            type: "string",
                            //validation: { required: true }

                        },
                        depCode: {
                            type: "string",
                            validation: { required: true }
                        },
                        estimatedDate:
                            {
                                type: "date",
                                //  validation: { required: true },
                                nullable: true,
                                value: "",
                                defaultOption: null
                            },

                        error: function (a) {
                            $('#productRequestGrid').data("kendoGrid").cancelChanges();
                        },
                    }
                }
            }


            
        });




        var statusdatasource = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "GET",

                        url: "../ScratchPad/GetStatus",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        data: JSON.stringify({ key: "value" }),
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });
        var involuedEmployees = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "GET",

                        url: "../ProductRequestGrid/GetInvlouedEmployees",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });
        var productsdataSource = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "GET",

                        url: "../ProductRequestGrid/GetProducts",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        data: JSON.stringify({ key: "value" }),
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });
        var departmentdataSource = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "GET",

                        url: "../ProductRequestGrid/GetDepartments",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        data: JSON.stringify({ key: "value" }),
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });
        var projectManagerdataSource = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "GET",

                        url: "../ProductRequestGrid/GetProjectManagers",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        data: JSON.stringify({ key: "value" }),
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });
        var leadDevelopersdataSource = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "GET",

                        url: "../ProductRequestGrid/GetLeadDevelopers",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        data: JSON.stringify({ key: "value" }),
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });
        var paidBydataSource = new kendo.data.DataSource({
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "GET",

                        url: "../ProductRequestGrid/PaidBy",
                        contentType: "application/json; charset=utf-8",
                        dataType: 'json',
                        data: JSON.stringify({ key: "value" }),
                        success: function (data) {
                            options.success(data);
                        }
                    });
                }
            }
        });

        function dateTimeEditor(container, options) {
            $('<input data-text-field="' + options.field + '" data-value-field="' + options.field + '" data-bind="value:' + options.field + '" data-format="' + options.format + '"/>')
            .appendTo(container)
            .kendoDateTimePicker({});
        }
        $("#ProductHistoryRequestGrid").kendoGrid({
            dataSource: dataSource2,
            scrollable: true,
            height: "300px",
            toolbar: [ "excel"], //, "save", "cancel"],
            excel: {
                allPages: true,
                fileName: "ProjectsExport.xlsx"
            },
          
            scrollable: true,
            height: 250,
            columns: [

   
     { field: "companyname", title: "Company Name ", filterable: true, width: "12em" },
            { field: "name", title: "Project Name *", filterable: true, width: "10em" },
            { field: "product", title: "Product *", filterable: true, editor: productsDropDownEditor, width: "8em" },
            { field: "depCode", title: "Department *", hidden: true, editor: departmentDropDownEditor, filterable: false, width: "10em" },
            { field: "projectManagerName", title: "ProjectManager *", hidden: true, editor: projectManagerDropDownEditor, filterable: false, width: "12em" },
            { field: "leadDeveloperName", title: "Lead Developer *", filterable: true, editor: leadDevelopersDropDownEditor, width: "12em" },
             { field: "daysToFinish", title: "Days To Finish", filterable: true, width: "10em" },
           { field: "depositAmount", title: "Deposit", format: "{0:c}", hidden: true },

          { field: "estimatedCost", title: "Estimated Cost", format: "{0:c}", width: "10em" },
             { field: "actualAmount", title: "Actual Cost", format: "{0:c}", hidden: true, width: "6em" },
                  { field: "statusName", title: "Status *", editor: statusDropDownEditor, width: "8em" },
                   {
                       field: "paidInFull", title: "Full Paid ?", template: '<input type="checkbox" #=paidInFull ? \'checked="checked"\' : "" # class="chkbx" onclick="return false;" />',
                       filterable: {
                           messages: {
                               isTrue: "Active", // sets the text for "isTrue" radio button
                               isFalse: "Inactive",
                           }
                       },
                       width: "10em"
                   },

                   {
                       field: "taskScheduleComplete", title: "Task Schedule Complete  ?", template: '<input type="checkbox" #=taskScheduleComplete ? \'checked="checked"\' : "" # class="chkbx" onclick="return false;" />',
                       filterable: {
                           messages: {
                               isTrue: "Active", // sets the text for "isTrue" radio button
                               isFalse: "Inactive",
                           }
                       },
                       width: "12em"
                   },

                     { field: "paidByName", title: "Paid By *", editor: paidByDropDownEditor, width: "9em" },

                        { field: "CommEmployee", title: "Invloued Employees *", editor: invlouedEmployeeDropDownEditor, width: "10em" },
                    {
                        field: "contractSentDate", title: "Contract Sent", hidden: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                        editor: dateTimeEditor,
                        filterable: {
                            ui: "datetimepicker"
                        }
                    },
                      {
                          field: "contractSignedDate", title: "Contract Signed", hidden: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                          editor: dateTimeEditor,
                          filterable: {
                              ui: "datetimepicker"
                          }
                      },
                         {
                             field: "depositReceivedDate", title: "Deposit Received", hidden: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                             editor: dateTimeEditor,
                             filterable: {
                                 ui: "datetimepicker"
                             }
                         },
                            {
                                field: "startDate", title: "Start Date", filterable: false, editable: true, format: "{0:MM/dd/yyyy HH:mm tt}",
                                editor: dateTimeEditor, width: "8em",
                                filterable: {
                                    ui: "datetimepicker"
                                }
                            },
                               {
                                   field: "expectedDeliveryDate", title: "Expected Delivery", filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                                   editor: dateTimeEditor, width: "8em",
                                   filterable: {
                                       ui: "datetimepicker"
                                   }
                               },

                                {
                                    field: "deliveryDate", title: "Delivery", hidden: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                                    editor: dateTimeEditor,
                                    filterable: {
                                        ui: "datetimepicker"
                                    }
                                },

                                 {
                                     field: "goLiveDate", title: "Go Live", hidden: true, editable: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                                     editor: dateTimeEditor,
                                     filterable: {
                                         ui: "datetimepicker"
                                     }
                                 },

                                 {
                                     field: "estimateDate", title: "Estimated Date", hidden: true, editable: true, filterable: false, format: "{0:MM/dd/yyyy HH:mm tt}",
                                     editor: dateTimeEditor, width: "8em",
                                     filterable: {
                                         ui: "datetimepicker"
                                     }
                                 },
                 {
                     field: "note", title: "Notes", width: "200px",
                     attributes: {
                         style: 'white-space: nowrap '
                     },
                     editor: function (container, options) {
                         $('<textarea data-bind="value: ' + options.field + '" cols="20" rows="10"></textarea>').appendTo(container);
                     }
                 },

          {
              title: "&nbsp;",
              width: "10em",


          }
            ],
            filterable: {
                extra: false,
                operators: {
                    string: {
                        eq: "Is equal to",
                        neq: "Is not equal to",
                        contains: "Contains"
                    }
                }
            },

            editable: "popup",

            sortable: {
                mode: "single",
                allowUnsort: false
            },
            selectable: true,
            edit: function () {
                //   e.container.find(".k-edit-label:nth-child(3)").hide();
                gridInEditMode = true;
            },
            excelExport: function (e) {
                for (var i = 0; i < e.workbook.sheets[0].columns.length; i++) {
                    e.workbook.sheets[0].columns[i].autoWidth = true;
                }
            },
            cancel: function () {
                gridInEditMode = false;
            },
            edit: function (e) {
                debugger;
                e.container.find(".k-edit-label:nth-child(1)").hide();
                e.container.find(".k-edit-field:nth-child(1)").hide();
                e.container.find(".k-edit-label:nth-child(2)").hide();
                e.container.find(".k-edit-field:nth-child(2)").hide();
                e.container.find(".k-edit-label:nth-child(3)").hide();
                e.container.find(".k-edit-field:nth-child(3)").hide();
                e.container.find(".k-edit-label:nth-child(4)").hide();
                e.container.find(".k-edit-field:nth-child(4)").hide();
                e.container.find(".k-edit-label:nth-child(5)").hide();
                e.container.find(".k-edit-field:nth-child(5)").hide();
                e.container.find(".k-edit-label:nth-child(6)").hide();
                e.container.find(".k-edit-field:nth-child(6)").hide();
                e.container.find(".k-edit-label:nth-child(7)").hide();
                e.container.find(".k-edit-field:nth-child(8)").hide();
                gridInEditMode = true;
                //e.container.find("input:first").hide();
                editedUID = e.model.uid;
                assignedEmployeeIdVar = e.model.CommEmployeeIds;
                $('<a href="../AddProduct/ProductHistory"><img id="theImg" src="https://cdn0.iconfinder.com/data/icons/16x16-free-toolbar-icons/16/13.png" /></a>').insertAfter($("#ProductValue").parent());
                $('<a href="../AddDepartment/DepartmentHistory"><img id="theImg" src="https://cdn0.iconfinder.com/data/icons/16x16-free-toolbar-icons/16/13.png" /></a>').insertAfter($("#departmentId").parent());


            }
        });

    $("#ProductHistoryRequestGrid").kendoTooltip({
            filter: "td:nth-child(27)", //this filter selects the second column's cells
            position: "right",
            content: function (e) {
                var target = e.target;
                return $(target).text();

            }
        }).data("kendoTooltip");

        dataSource2.bind("requestEnd", showSuccessMessage);
        $(".close").click(function () {
            $(this).parent().removeClass('in');
        });
        function showSuccessMessage(e) {
            if (e.type === "update" || e.type === "create") {
                gridInEditMode = false;
                $("#saveConfirm").addClass('in');
                e.sender.read();
            }
        }
        function productsDropDownEditor(container, options) {

            $('<input id="ProductValue" data-text-field="product" data-value-field="id"  data-bind="value:productId"/>').appendTo(container).kendoDropDownList({
                autoBind: true,
                dataValueField: "id",
                dataTextField: "product",
                optionLabel: "select a product",
                dataSource: productsdataSource
            });
            $('<span class="k-invalid-msg" data-for="product"></span>').appendTo(container);

        }
        function invlouedEmployeeDropDownEditor(container, options) {
            $('<input id="invlouedEmployeeId"  data-bind="value:CommEmployeeIds" />').appendTo(container).kendoDropDownList({
                autoBind: false,
                dataValueField: "id",
                dataTextField: "InvoluedEname",
                dataSource: involuedEmployees,
                template: "<input type='checkbox' id='chk_AssEmp_#=data.id #' onclick='UpdateIdinIsAssEmp(this);' value='#=data.id #' name='chk_AssEmp' />" + " " + "${ data.InvoluedEname }",
                close: function (e) {
                    if (IsAssEmpChecked == true) {
                        IsAssEmpChecked = false;
                        e.preventDefault();
                    }
                    else {
                        var allVals = [];
                        $("input[id*='chk_AssEmp']:checked").each(function () {
                            allVals.push($(this).val());
                        });

                        var gridList = $("#ProductRequestGrid").data("kendoGrid").dataSource.data();
                        for (var i = 0; i < gridList.length; i++) {
                            if (gridList[i].uid == editedUID) {
                                gridList[i].CommEmployeeIds = allVals.join(",");
                                break;
                            }
                        }

                        e.sender.text(allVals.length + " item(s) selected");
                    }
                },
                change: function (e) {
                    e.sender.value(null);
                },
                dataBound: function (e) {
                    //alert("assignedEmployeeIdVar");
                    var count = 0;
                    if (assignedEmployeeIdVar) {
                        assignedEmployeeIdVar = "," + assignedEmployeeIdVar + ",";
                        $("input[id*='chk_AssEmp']").each(function () {

                            if (assignedEmployeeIdVar.indexOf("," + $(this).val() + ",") != -1) {

                                $(this).attr('checked', 'checked');
                                count++;
                            }
                        });
                        assignedEmployeeIdVar = null;
                        e.sender.text(count + " item(s) selected");
                    }
                },
            });
        }
        function departmentDropDownEditor(container, options) {
            $('<input id="departmentId" data-text-field="depCode" data-value-field="departmentId" data-bind="value:departmentId"/>').appendTo(container).kendoDropDownList({
                autoBind: true,
                dataValueField: "departmentId",
                dataTextField: "depCode",
                optionLabel: "select a department",
                dataSource: departmentdataSource

            });
            $('<span class="k-invalid-msg" data-for="depCode"></span>').appendTo(container);
        }
        function projectManagerDropDownEditor(container, options) {
            $('<input id="ManagerId" data-text-field="projectManagerName" data-value-field="id"  data-bind="value:projectManagerId"/>').appendTo(container).kendoDropDownList({
                autoBind: true,
                dataValueField: "id",
                optionLabel: "select project manager",
                dataTextField: "projectManagerName",
                dataSource: projectManagerdataSource

            });

        }
        function leadDevelopersDropDownEditor(container, options) {
            $('<input id="leaddeveloper" data-text-field="leadDeveloperName" data-value-field="id"  data-bind="value:leadProgrammerId"/>').appendTo(container).kendoDropDownList({
                autoBind: true,
                dataValueField: "id",
                dataTextField: "leadDeveloperName",
                optionLabel: "select lead developer",
                dataSource: leadDevelopersdataSource

            });
        }
        function paidByDropDownEditor(container, options) {
            $('<input id="paidId" data-text-field="paidByName" data-value-field="id" data-bind="value:paidById"/>').appendTo(container).kendoDropDownList({
                autoBind: true,
                dataValueField: "id",
                dataTextField: "paidByName",
                dataSource: paidBydataSource

            });
        }
        function statusDropDownEditor(container, options) {
            $('<input id="leaddeveloper" data-text-field="statusName" data-value-field="status"  data-bind="value:statusId"/>').appendTo(container).kendoDropDownList({
                autoBind: true,
                dataValueField: "status",
                dataTextField: "statusName",
                optionLabel: "select status",
                dataSource: statusdatasource

            });
            $('<span class="k-invalid-msg" data-for="statusName"></span>').appendTo(container);
        }



  //  }

</script>

